from fastapi import APIRouter
from pydantic import BaseModel

api_user  = APIRouter()

class User(BaseModel):
    id:int
    name:str
    gender:str|None = '未知'
    msg:str|None = None

users = []

@api_user.get('/')
def findAll():
    return users

#查 GET http://127.0.0.1:8000/user/1
@api_user.get('/{id}')
def getUser(id:int):
    for user in users:
        if user.id == id:
            return user

    return "查不到"
#增 POST http://127.0.0.1:8000/user
@api_user.post("/")
def addUser(user:User):

    users.append(user)

    user.msg = '增加数据'

    return user
#删 DELETE http://127.0.0.1:8000/user/1
@api_user.delete("/{id}")
def delUser(id):
    return "删除"
#改 PUT http://127.0.0.1:8000/user
@api_user.put("/")
def modUser(user:User):

    user.msg = '改的数据'

    return user